#ifndef BUTTONGROUP_H
#define BUTTONGROUP_H

#include <QObject>
#include "../../XXqtDefine.h"
#include <QAbstractButton>
#include <QMap>

#include <QButtonGroup>
#include <QPushButton>

class ButtonGroup : public QObject
{
    Q_OBJECT
public:
    explicit ButtonGroup(QObject *parent = nullptr);

    void shell(QWidget *container);

    // config
    void setSelectable(bool selectable);
    void setExclusive(bool exclusive, bool canUnSelectWhenOnlyOne);

    // action
    template<typename T>
    T* add(const QString &name, const QString &text = ""){
        T *obj = new T(_widget);
        obj->setObjectName(name);
        obj->setText(text);
        bool ret = add(name,obj);
        if(!ret){
            delete obj;
            return nullptr;
        }
        return obj;
    }
    bool add(const QString &name, QAbstractButton *button);
    void remove(const QString &name);
    void del(const QString &name);
    QAbstractButton* take(const QString &name);

    void select(const QString &name);
    void unselect(const QString &name);
    QStringList currentSelected();

    // signal
signals:
    void sigClicked(QString name);
    void sigSelectionChanged(QString current, QString previous);
    void sigSelectionChanged(QStringList current, QStringList previous);

private:
    QWidget* widget();
    void setCurrent(const QString &name);
    void addToCurrent(const QString &name);
    void removeFromCurrent(const QString &name);
    QStringList getCurrent();

private slots:
    void onButtonClicked(bool checked);

private:
    QWidget *_widget;
    QMap<QString,QAbstractButton*> _nameToButton;
    bool _selectable;
    bool _exclusive;
    bool _canUnSelectWhenOnlyOne;

    QStringList _current;
};

#endif // BUTTONGROUP_H
